package com.aidong.service;

/**
 * Created by john on 16-11-3.
 */
import com.aidong.dao.wills.SysMessageDao;
import com.aidong.dao.wills.SysUserLogDao;
import com.aidong.model.LoginUser;
import com.aidong.model.MenuAuthority;
import com.aidong.model.RequestInfo;
import com.aidong.model.wills.DicShopCode;
import com.aidong.model.wills.SysUser;
import com.aidong.model.wills.SysUserLog;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

@Service
public class SysUserSession implements Runnable {
    protected Logger log = Logger.getLogger(this.getClass());
    @Resource
    protected SysUserLogDao sysUserLogDao;
    @Resource
    protected SysMessageDao sysMessageDao;
    protected final Map<String, LoginUser> userMap = new HashMap();
    protected ThreadLocal<LoginUser> userTL = new ThreadLocal();
    protected ThreadLocal<MenuAuthority> authTL = new ThreadLocal();
    protected ThreadLocal<RequestInfo> reqTL = new ThreadLocal();
    private Thread thread = new Thread(this);

    public SysUserSession() {
        this.thread.setDaemon(true);
        this.thread.start();
    }

    public void run() {
        while(true) {
            try {
                this.chkLoginUser();
            } catch (Exception var2) {
                this.log.error("SysUserSession thread run error", var2);
            }

            this.sleep();
        }
    }

    private void sleep() {
        try {
            Thread.sleep(60000L);
        } catch (InterruptedException var2) {
            this.log.info(var2.getMessage());
        }

    }

    private void chkLoginUser() {
        Map var1 = this.userMap;
        synchronized(this.userMap) {
            ArrayList ulist = new ArrayList();
            ulist.addAll(this.userMap.values());
            Iterator i$ = ulist.iterator();

            while(true) {
                LoginUser user;
                do {
                    if(!i$.hasNext()) {
                        return;
                    }

                    user = (LoginUser)i$.next();
                } while(System.currentTimeMillis() - user.getAccessTime() <= 300000L && (user.getLoginStatus() == 1 || System.currentTimeMillis() - user.getAccessTime() <= 60000L));

                this.addLogoutLog(user);
                this.userMap.remove(user.getSessionId());
            }
        }
    }

    public void addLogoutLog(LoginUser user) {
        try {
            SimpleDateFormat e = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            SysUserLog userLog = new SysUserLog();
            userLog.setLogTime(new Date());
            userLog.setUserCode(user.getUserCode());
            userLog.setShopCode(user.getShopCode());
            userLog.setOperate(user.getLoginStatus() == 1?"超时退出":"强制退出");
            userLog.setContent("登录时间:" + e.format(new Date(user.getLoginTime())));
            userLog.setIpAddr(user.getIpAddr());
            this.sysUserLogDao.add(userLog);
        } catch (Exception var4) {
            this.log.error("addUserLog", var4);
        }

    }

    public LoginUser addLoginUser(SysUser sysUser, DicShopCode shop, HttpServletRequest request) {
        LoginUser user = new LoginUser(sysUser);
        user.setLoginShop(shop);
        user.setMsgNum(this.sysMessageDao.getNewMsgNum(user.getUserCode(), user.getShopCode()));
        user.setSessionId(request.getSession().getId());
        user.setLoginTime(System.currentTimeMillis());
        user.updateAccessTime();
        user.setLoginStatus(1);
        RequestInfo req = new RequestInfo(request);
        user.setIpAddr(req.getIpAddr());
        user.setOsType(req.getOsType());
        Map var6 = this.userMap;
        synchronized(this.userMap) {
            if(sysUser.getLoginSingle() != null && sysUser.getLoginSingle().intValue() == 1) {
                Iterator i$ = this.userMap.values().iterator();

                while(i$.hasNext()) {
                    LoginUser u = (LoginUser)i$.next();
                    if(u.getUserCode().equalsIgnoreCase(user.getUserCode())) {
                        u.setLoginStatus(3);
                    }
                }
            }

            this.userMap.put(request.getSession().getId(), user);
        }

        this.userTL.set(user);
        return user;
    }

    public void removeLoginUser(HttpServletRequest request) {
        this.userMap.remove(request.getSession().getId());
        this.userTL.remove();
    }

    public void setLoginInfo(HttpServletRequest request) {
        LoginUser user = (LoginUser)this.userMap.get(request.getSession().getId());
        if(user != null) {
            user.updateAccessTime();
        }

        this.userTL.set(user);
        RequestInfo req = new RequestInfo(request);
        this.reqTL.set(req);
    }

    public void updateUserMsgNum() {
        LoginUser user = (LoginUser)this.userTL.get();
        if(user != null) {
            user.setMsgNum(this.sysMessageDao.getNewMsgNum(user.getUserCode(), user.getShopCode()));
        }

    }

    public LoginUser getLoginUser(String sessionId) {
        return (LoginUser)this.userMap.get(sessionId);
    }

    public List<LoginUser> getLoginUserList() {
        ArrayList list = new ArrayList();
        list.addAll(this.userMap.values());
        return list;
    }

    public LoginUser getLoginUser() {
        return (LoginUser)this.userTL.get();
    }

    public void setAuthority(MenuAuthority authority) {
        this.authTL.set(authority);
    }

    public MenuAuthority getAuthority() {
        return (MenuAuthority)this.authTL.get();
    }

    public boolean chkAuthority(String op) {
        MenuAuthority auth = (MenuAuthority)this.authTL.get();
        return auth == null?false:auth.chkAuth(op);
    }

    public RequestInfo getRequestInfo() {
        return (RequestInfo)this.reqTL.get();
    }

    public void remove() {
        this.userTL.remove();
        this.authTL.remove();
        this.reqTL.remove();
    }
}

